﻿using Server.Constant;
using Server.Enum;
using Server.Framework.Message;
using Server.Framework.Processor;
using Server.Framework.Service;
using Server.Framework.Socket;
using Server.Message.Request;
using Server.Message.Response;
using Server.Model;
using Server.OptExp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Server.Processor
{
    /// <summary>
    /// Author: Leil
    /// Description: 顶级菜单列表请求处理
    /// CreateTime: 2020/5/31 1:09:15
    /// </summary>
    [ProcessorAttr(MessageId.REQUEST_MENUS_TREE)]
    class RequestMenusTreeProcessor : IProcessor
    {
        public void Process(ProtobufAppSession session, RequestBaseMessage message)
        {
            if (message is MenusTreeReq msg)
            {
                if (msg.IsAll == 1)
                {
                    GetMenusTree(session, msg);
                } else
                {
                    GetMenusTreeByRole(session, msg);
                }
            }
        }



        private void GetMenusTree(ProtobufAppSession session, MenusTreeReq msg)
        {
            if (msg.RoleId == 0)
            {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP)
                {
                    Code = 500,
                    OptType = (int)OptType.QUERY_LIST,
                    Content = "角色ID不能为空"
                };
                session.Send(resp);
                return;
            }

            string sql = $@"SELECT
	* 
FROM
	SYS_MENU
	LEFT JOIN ( SELECT * FROM SYS_ROLE_MENU WHERE ROLE_ID = {msg.RoleId} ) roleMenus ON SYS_MENU.ID = roleMenus.MENU_ID 
WHERE
	SYS_MENU.DEL_FLAG = 1 
ORDER BY
	SORT DESC";
            try
            {
                DBService.Instance().ExecuteQuery(sql, reader =>
                {
                    MenusTreeResp resp = new MenusTreeResp(MessageId.RESPONSE_MENUS_TREE)
                    {
                        IsAll = msg.IsAll
                    };
                    while (reader.Read())
                    {
                        MenuDao menu = new MenuDao()
                        {
                            ID = Convert.ToInt32(reader["ID"]),
                            ParentId = Convert.ToInt32(reader["PARENT_ID"]),
                            MenuName = reader["MENU_NAME"].ToString(),
                            Icon = reader["ICON"].ToString(),
                            ScreenCode = reader["SCREEN_CODE"].ToString(),
                            CheckedType = reader["CHECKED_TYPE"] == DBNull.Value ? 2 : Convert.ToInt32(reader["CHECKED_TYPE"])
                        };
                        resp.Menus.Add(menu);
                    }
                    session.Send(resp);
                });
            }
            catch (CommonOptException e)
            {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP)
                {
                    Code = 500,
                    OptType = (int)OptType.QUERY_LIST,
                    Content = e.Message
                };
                session.Send(resp);
            }
        }


        private void GetMenusTreeByRole(ProtobufAppSession session, MenusTreeReq msg)
        {
            if (session.User == null)
            {
                Console.WriteLine("用户未登录");
                return;
            }
            string sql = $@"SELECT
	* 
FROM
	SYS_MENU
	INNER JOIN (
SELECT
	MENU_ID 
FROM
	SYS_ROLE_MENU
	INNER JOIN ( SELECT * FROM SYS_USER INNER JOIN SYS_USER_ROLE ON SYS_USER.ID = SYS_USER_ROLE.USER_ID WHERE SYS_USER.ID = {session.User.ID} ) userRoles ON SYS_ROLE_MENU.ROLE_ID = userRoles.ROLE_ID 
WHERE
	SYS_ROLE_MENU.CHECKED_TYPE = 1 
	OR SYS_ROLE_MENU.CHECKED_TYPE = 3 
	) roleMenus ON SYS_MENU.ID = roleMenus.MENU_ID 
WHERE
	SYS_MENU.DEL_FLAG = 1 
	AND STATUS = 1
ORDER BY SORT DESC
";
            try
            {
                DBService.Instance().ExecuteQuery(sql, reader =>
                {
                    MenusTreeResp resp = new MenusTreeResp(MessageId.RESPONSE_MENUS_TREE)
                    {
                        IsAll = msg.IsAll
                    };
                    while (reader.Read())
                    {
                        MenuDao menu = new MenuDao()
                        {
                            ID = Convert.ToInt32(reader["ID"]),
                            ParentId = Convert.ToInt32(reader["PARENT_ID"]),
                            MenuName = reader["MENU_NAME"].ToString(),
                            Icon = reader["ICON"].ToString(),
                            ScreenCode = reader["SCREEN_CODE"].ToString(),
                        };
                        resp.Menus.Add(menu);
                    }
                    session.Send(resp);
                });
            }
            catch (CommonOptException e)
            {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP)
                {
                    Code = 500,
                    OptType = (int)OptType.QUERY_LIST,
                    Content = e.Message
                };
                session.Send(resp);
            }
        }





    }
}
